/* (swing1.1beta3) */
package com.hardson.toolkit.swing.table;

import java.awt.Component;
import java.awt.Dimension;
import java.util.Enumeration;
import java.util.Vector;

import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;

/**
 * ColumnGroup.
 *
 * @author Nobuo Tamemasa
 * @version 1.0 10/20/98
 */
class ColumnGroup {

	protected TableCellRenderer renderer;

	protected Vector<Object> v;

	protected String text;

	protected int margin = 0;

	public ColumnGroup(final String text) {
		this(null, text);
	}

	public ColumnGroup(final TableCellRenderer renderer, final String text) {
		if (renderer == null) {
			this.renderer = new DefaultTableCellRenderer() {
				private static final long serialVersionUID = 1L;

				@Override
				public Component getTableCellRendererComponent(final JTable table,
						final Object value, final boolean isSelected, final boolean hasFocus,
						final int row, final int column) {
					JTableHeader header = table.getTableHeader();
					if (header != null) {
						this.setForeground(header.getForeground());
						this.setBackground(header.getBackground());
						this.setFont(header.getFont());
					}
					this.setHorizontalAlignment(SwingConstants.CENTER);
					this.setText((value == null) ? "" : value.toString());
					this.setBorder(UIManager.getBorder("TableHeader.cellBorder"));
					return this;
				}
			};
		} else {
			this.renderer = renderer;
		}
		this.text = text;
		this.v = new Vector<>();
	}

	/**
	 * param obj TableColumn or ColumnGroup
	 */
	//@SuppressWarnings("unchecked")
	public void add(final Object obj) {
		if (obj == null) {
			return;
		}
		this.v.addElement(obj);
	}

	/**
	 * param c TableColumn param v ColumnGroups
	 */
	//@SuppressWarnings("unchecked")
	public Vector<ColumnGroup> getColumnGroups(final TableColumn c, final Vector<ColumnGroup> g) {
		g.addElement(this);
		if (this.v.contains(c)) {
			return g;
		}
		Enumeration<Object> em = this.v.elements();
		while (em.hasMoreElements()) {
			Object obj = em.nextElement();
			if (obj instanceof ColumnGroup) {
				@SuppressWarnings("unchecked")
				Vector<ColumnGroup> groups = ((ColumnGroup) obj).getColumnGroups(c,
						(Vector<ColumnGroup>) g.clone());
				if (groups != null) {
					return groups;
				}
			}
		}
		return null;
	}

	public TableCellRenderer getHeaderRenderer() {
		return this.renderer;
	}

	public void setHeaderRenderer(final TableCellRenderer renderer) {
		if (renderer != null) {
			this.renderer = renderer;
		}
	}

	public Object getHeaderValue() {
		return this.text;
	}

	public Dimension getSize(final JTable table) {
		Component comp = this.renderer.getTableCellRendererComponent(table, this.getHeaderValue(),
				false, false, -1, -1);
		int height = comp.getPreferredSize().height;
		int width = 0;
		Enumeration<Object> em = this.v.elements();
		while (em.hasMoreElements()) {
			Object obj = em.nextElement();
			if (obj instanceof TableColumn) {
				TableColumn aColumn = (TableColumn) obj;
				//				if(GroupHeader.EMPTY_LABEL.equals(aColumn.getHeaderValue())){
				//					height=comp.getPreferredSize().height+aColumn.getHeaderRenderer()
				//						.getTableCellRendererComponent(table,
				//								getHeaderValue(), false, false, -1, -1).getPreferredSize().height;
				//					//height=
				//				}
				width += aColumn.getWidth();
				width += this.margin;
			} else {
				width += ((ColumnGroup) obj).getSize(table).width;
			}
		}
		return new Dimension(width, height);
	}

	public void setColumnMargin(final int margin) {
		this.margin = margin;
		Enumeration<Object> em = this.v.elements();
		while (em.hasMoreElements()) {
			Object obj = em.nextElement();
			if (obj instanceof ColumnGroup) {
				((ColumnGroup) obj).setColumnMargin(margin);
			}
		}
	}
}
